﻿using ASmile.DBModel;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmSelectUser : FrmBaseModal
    {
        public FrmSelectUser()
        {
            InitializeComponent();
            AutoInitGridView = true;
        }

        static FrmSelectUser _Instance;

        public static FrmSelectUser Create(Action<Sys_User> AddAction)
        {
            if (_Instance == null || _Instance.IsDisposed)
            {
                _Instance = new FrmSelectUser();
                _Instance.AddHandle = AddAction;
            }
            return _Instance;
        }

        public Action<Sys_User> AddHandle;

        ArgPageData SearchArg { get; } = new ArgPageData();

        void Init()
        {
            dgv.AddButtonColumn("添加", AddHandle);
            ucPageNav1.PageChangedEvent += OnPageChanged;
            
        }

        void OnPageChanged(int pageNo, int pageSize, out int total)
        {
            SearchArg.SearchVal = tsTxtSearch.Text;
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            var ret = ComData.M.SearchUsers(SearchArg);
            total = ret.count;
            dgv.DataSource = ret.data;
        }

        void RefreshData()
        {
            OnPageChanged(1, ucPageNav1.OnePageCount, out int total);
            ucPageNav1.InitiControl(total);
        }

        private void FrmSelectUser_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshData();
            }
        }

        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}